﻿<Window x:Class="CodeAngel.UI.EntityDefWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:CodeAngel.UI"
        xmlns:domain="clr-namespace:CodeAngel.Domain;assembly=CodeAngel.Domain"
        Title="EntityDefWindow" Height="400" Width="720" WindowStartupLocation="CenterOwner">
    
    <Window.Resources>

        <system:Double x:Key="LABEL_WIDTH">80.0</system:Double>

        <HierarchicalDataTemplate DataType="{x:Type local:FilterRelationItem}" ItemsSource="{Binding Children}" >
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding IsChecked}" Visibility="{Binding IsCheckedVisibility}" />
                <TextBlock Text="{Binding Header}" />
            </StackPanel>
        </HierarchicalDataTemplate>

        <Style TargetType="TreeViewItem" BasedOn="{StaticResource {x:Type TreeViewItem}}">
            <Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
        </Style>

    </Window.Resources>

    <Grid Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        
        <!-- Scalar Properties -->
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            
            <StackPanel >
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <local:LabeledTextBox Label="Name" Text="{Binding EntityDef.Name}" LabelWidth="{StaticResource LABEL_WIDTH}" />
                    <local:LabeledCheckBox Label="Is Keyword" IsChecked="{Binding EntityDef.IsNameKeyword}" Grid.Column="1" Margin="6,0,0,0"/>
                </Grid>
                <!--<local:LabeledTextBox Label="Sub-Folder" Text="{Binding EntityDef.SubFolder}" LabelWidth="{StaticResource LABEL_WIDTH}" />-->
                <local:LabeledComboBox x:Name="SubFolderComboBox" Focusable="False" Label="Subfolder" Text="{Binding EntityDef.SubFolder, Mode=TwoWay}" LabelWidth="{StaticResource LABEL_WIDTH}" IsEditable="True"
                                       ItemsSource="{Binding DistinctSubFolders}"/>
                <local:LabeledCheckBox Label="Can be Root" IsChecked="{Binding EntityDef.CanBeAggregateRoot}" LabelWidth="{StaticResource LABEL_WIDTH}"/>
            </StackPanel>

            <local:LabeledTextBox Label="Purpose" Text="{Binding EntityDef.Purpose}" LabelWidth="{StaticResource LABEL_WIDTH}" Grid.Row="1"/>

            <StackPanel Grid.Row="2">
                <local:LabeledTextBox Label="Friendly Name" Text="{Binding EntityDef.FriendlyName}" LabelWidth="{StaticResource LABEL_WIDTH}" />
                <local:LabeledTextBox Label="Table Name" Text="{Binding EntityDef.TableName}" LabelWidth="{StaticResource LABEL_WIDTH}" />                
            </StackPanel>
        </Grid>

        <!-- Collection Properties -->
        <TabControl Grid.Column="1" Margin="6,0,0,0" >
            <TabItem Header="Property Definitions">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <!-- Toolbar -->
                    <Border BorderBrush="Black" BorderThickness="1">
                        <StackPanel Orientation="Horizontal">
                            <Button Content="New" Command="{Binding CreateNewPropertyDefCommand}" />
                            <Button Content="Add PK" Command="{Binding CreatePrimaryKeyPropertyDefCommand}" Margin="6,0,0,0" />
                            <Button Content="Add Name" Command="{Binding CreateNamePropertyDefCommand}" Margin="6,0,0,0" />
                            <Button Content="Add FriendlyID" Command="{Binding CreateFriendlyIDPropertyDefCommand}" Margin="6,0,0,0" />
                            <Button Content="Add Notes" Command="{Binding CreateNotesPropertyDefCommand}" Margin="6,0,0,0" />
                        </StackPanel>
                    </Border>

                    <ItemsControl x:Name="PropertyDefsListBox" ItemsSource="{Binding PropertyDefsCollectionView}" Grid.Row="1">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <local:ListItemControl OpenCommand="{Binding ElementName=PropertyDefsListBox, Path=DataContext.OpenPropertyDefCommand}"
                                                       DeleteCommand="{Binding ElementName=PropertyDefsListBox, Path=DataContext.DeletePropertyDefCommand}"
                                                       MarkedAsDeleted="{Binding FlaggedAsDeleted, Mode=TwoWay}">
                                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="3">
                                        <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                                        <TextBlock Text="{Binding DataType}" Margin="6,0,0,0" />
                                    </StackPanel>
                                </local:ListItemControl>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Grid>
            </TabItem>

            <TabItem Header="Aggregates">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <!-- Toolbar -->
                    <Border BorderBrush="Black" BorderThickness="1">
                        <StackPanel Orientation="Horizontal">
                            <Button Content="New" Command="{Binding CreateNewAggregateDefCommand}" />
                        </StackPanel>
                    </Border>

                    <ItemsControl x:Name="AggregateDefsListBox" ItemsSource="{Binding AggregateDefsCollectionView}" Grid.Row="1">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <local:ListItemControl OpenCommand="{Binding ElementName=AggregateDefsListBox, Path=DataContext.OpenAggregateDefCommand}"
                                                       DeleteCommand="{Binding ElementName=AggregateDefsListBox, Path=DataContext.DeleteAggregateDefCommand}"
                                                       MarkedAsDeleted="{Binding FlaggedAsDeleted, Mode=TwoWay}">
                                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="3">
                                        <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                                    </StackPanel>
                                </local:ListItemControl>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Grid>
            </TabItem>

            <TabItem x:Name="FilterTabItem" Header="Filter" Visibility="{Binding EntityDef.CanBeAggregateRoot, Converter={StaticResource TrueToVisibleConverter}}">
                <TreeView>
                    <TreeViewItem IsExpanded="True" 
                                  ItemsSource="{Binding RootFilterRelationItem.Children}" >
                        <TreeViewItem.Header>
                            <TextBlock Text="{Binding RootFilterRelationItem.Header}" />
                        </TreeViewItem.Header>
                    </TreeViewItem>
                        
                </TreeView>
            </TabItem>
            
        </TabControl>
        
        
        <!-- Save and Cancel Buttons -->
        <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Right" Margin="0,6,0,0">
            <Button Content="OK" Click="OkHandler" />
            <Button Content="Cancel" IsCancel="True" Margin="6,0,0,0" />
        </StackPanel>
        
    </Grid>
</Window>

